﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Reader.Core
{
    public static class Extension
    {
        public static async Task WriteFileStreamAsync(this Stream reader, string filePath)
        {
            using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                await reader.CopyToAsync(fs);
            }
        }
        #region string
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static bool IsNullOrWhiteSpace(this string str)
        {
            return string.IsNullOrWhiteSpace(str);
        }
        public static int ToInt32(this string str)
        {
            int.TryParse(str, out int val);
            return val;
        }
        public static long ToInt64(this string str)
        {
            long.TryParse(str, out long val);
            return val;
        }
        public static T ToObject<T>(this string jsonString)
        {
            var options = new JsonSerializerOptions
            {
                ReadCommentHandling = JsonCommentHandling.Skip,
                AllowTrailingCommas = true
            };
            return JsonSerializer.Deserialize<T>(jsonString, options);
        }
        public static Match Match(this string str, string pattern, RegexOptions options)
        {
            return Regex.Match(str, pattern, options);
        }
        #endregion
    }
}
